#include "cBouton.h"

cBouton::cBouton(cHwnd * hwndParent,LPCSTR label,int x,int y,int largeur,int hauteur,cHwnd * HWNDParent):
    cControl(hwndParent)
{
    //ctor
    HWND leParent=hwndParent->getHwnd();
    if(HWNDParent!=NULL)
        leParent=HWNDParent->getHwnd();


    hWnd=CreateWindowEx(0,"BUTTON", label, WS_CHILD  | WS_VISIBLE | WS_TABSTOP ,
                      x, y, largeur, hauteur,  leParent, NULL, hInstance, NULL);
    FinalInit();
    OnClick=NULL;
    OnRightClick=NULL;
    foregroundColor=RGB(0,0,0);
}

cBouton::~cBouton()
{
    //dtor
}

void cBouton::SetFontColor(int r,int v,int b)
{
    foregroundColor=RGB(r,v,b);
}

bool cBouton::TraitementEvent(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,LRESULT & resultat)
{
    switch(message)
    {
        case WM_COMMAND:
        {
            if(HIWORD(wparam)==BN_CLICKED)
            {
                if(OnClick!=NULL)
                {
                    cEvent * event=new cEvent(this);
                    (_parent->*OnClick)(event);
                    delete (event);

                    resultat=0;
                    return true;
                }
            }
            break;
        }

        case WM_RBUTTONUP:
        {
            if(OnRightClick!=NULL)
            {
                cEvent * event=new cEvent(this);
                (_parent->*OnRightClick)(event);
                delete (event);
                return false;
            }
            break;
        }


        case WM_NOTIFY:
        {
            NMHDR * structnhdr=(NMHDR*)lparam;
            switch(structnhdr->code)
            {
                case NM_CUSTOMDRAW:
                {
                    LPNMCUSTOMDRAW lpNMCustomDraw = (LPNMCUSTOMDRAW) lparam;
                    switch(lpNMCustomDraw->dwDrawStage)
                    {
                        case CDDS_PREPAINT :
                        {

                            const int textLength = ::GetWindowTextLength(lpNMCustomDraw->hdr.hwndFrom);

                            if (textLength > 0)
                            {
                                TCHAR* buttonText = new TCHAR[textLength+1];
                                SIZE   dimensions = {0};

                                GetWindowText(lpNMCustomDraw->hdr.hwndFrom, buttonText, textLength+1);
                                GetTextExtentPoint32(lpNMCustomDraw->hdc, buttonText, textLength, &dimensions);

                                const int xPos = (lpNMCustomDraw->rc.right - dimensions.cx) / 2;
                                const int yPos = (lpNMCustomDraw->rc.bottom - dimensions.cy) / 2;

                                SetBkMode(lpNMCustomDraw->hdc, TRANSPARENT);
                                SetTextColor(lpNMCustomDraw->hdc,foregroundColor);
                                //TextOut(lpNMCustomDraw->hdc, xPos, yPos, buttonText, textLength);

                                RECT rect;
                                rect.left=0;
                                rect.top=yPos;
                                rect.right=lpNMCustomDraw->rc.right;
                                rect.bottom=lpNMCustomDraw->rc.bottom;

                                UINT style=DT_CENTER|DT_WORDBREAK;
                                if(!(GetKeyState(VK_MENU)&0x80))
                                {
                                    //style|=0x00100000;
                                }

                                DrawText(lpNMCustomDraw->hdc,buttonText, textLength,&rect,style );

                                delete [] buttonText;

                                resultat=CDRF_SKIPDEFAULT;
                                return true;
                            }
                            else{
                                resultat=CDRF_DODEFAULT;
                                return true;
                            }
                        }


                    };
                }


            };

            break;
        }
    };
    return false;
}

